/* tcpClient.c - TCP client example */ 
 
/* 
DESCRIPTION 
This file contains the client-side of the VxWorks TCP example code. 
The example code demonstrates the usage of several BSD 4.4-style 
socket routine calls. 
*/ 
 
/* includes */ 
 
#include "vxWorks.h" 
#include "sockLib.h" 
#include "inetLib.h" 
#include "stdioLib.h" 
#include "strLib.h" 
#include "hostLib.h" 
#include "ioLib.h" 
#include "tcpExample.h"
#include "tcpClient.h"
#include "msgQLib.h"
#include "globals.h"
 
/**************************************************************************** 
* 
* tcpClient - send requests to server over a TCP socket 
* 
* This routine connects over a TCP socket to a server, and sends a 
* user-provided message to the server.  Optionally, this routine 
* waits for the server's reply message. 
* 
* RETURNS: OK, or ERROR if the message could not be sent to the server. 
*/ 

static void lireMessage(struct request* Requete){
	/* 
	 * Read de the message in the message queue and build the request with it.
	 */
	char message[REQUEST_MSG_SIZE];
	msgQReceive(balMsgEnv, message, sizeof(message),WAIT_FOREVER);
	strcat(Requete->message,message);
	Requete->msgLen = strlen(message);
	Requete->reply = FALSE;
}
 
STATUS tcpClient 
	( 
	char *              serverName     /* name or IP address of server */ 
	) 
	{ 
	struct request      myRequest;     /* request to send to server */ 
	struct sockaddr_in  serverAddr;    /* server's socket address */ 
	char                replyBuf[REPLY_MSG_SIZE]; /* buffer for reply */ 
	char                reply;         /* if TRUE, expect reply back */ 
	int                 sockAddrSize;  /* size of socket address structure */ 
	int                 sFd;           /* socket file descriptor */ 
	int                 mlen;          /* length of message */ 
 
	finTache = 0;
	/* create client's socket */ 
	if ((sFd = socket (AF_INET, SOCK_STREAM, 0)) == ERROR) 
		{ 
		perror ("socket"); 
		return (ERROR); 
		} 
 
	/* bind not required - port number is dynamic */ 
	/* build server socket address */ 
	sockAddrSize = sizeof (struct sockaddr_in); 
	bzero ((char *) &serverAddr, sockAddrSize); 
	serverAddr.sin_family = AF_INET; 
	serverAddr.sin_len = (u_char) sockAddrSize; 
	serverAddr.sin_port = htons (JAVA_SERVER_PORT_NUM); 
 
	if (((serverAddr.sin_addr.s_addr = inet_addr (serverName)) == ERROR) && 
		((serverAddr.sin_addr.s_addr = hostGetByName (serverName)) == ERROR)) 
		{ 
		perror ("unknown server name"); 
		close (sFd); 
		return (ERROR); 
		} 
 
	/* connect to server */ 
	if (connect (sFd, (struct sockaddr *) &serverAddr, sockAddrSize) == ERROR) 
		{ 
		perror ("connect"); 
		close (sFd); 
		return (ERROR); 
		} 

	FOREVER
	{
		/* build request, prompting user for message */ 
		lireMessage(&myRequest);		
   
		/* send request to server */ 
	 
		if (write (sFd, (char *) &myRequest, sizeof (myRequest)) == ERROR) 
		{ 
			perror ("write"); 
			close (sFd); 
			return (ERROR); 
		} 
	}
	close (sFd); 
	return (OK); 
}

